// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

axios.defaults.baseURL="http://192.168.70.124:9090"
Vue.prototype.$http=axios
Vue.config.productionTip = false
Vue.use(ElementUI);

axios.interceptors.request.use(function (config) {
    let token = localStorage.getItem("token")
    if(token){
        config.headers["token"]=token;
    }
    return config;
});
axios.interceptors.response.use(function(config) {
  let code = config.data.code
  if (code == 401) {
    router.push("/adminLogin")
  }
  if (code == 403) {
    router.push("/noPermission")
  }
  let token=config.headers["token"]
  if(token){
    localStorage.setItem("token",token)
  }
  return config;
});

/**
 * 路由守卫
 */
router.beforeEach((to,from,next)=>{
    let path = to.path
    if(path=="/adminLogin"||path=="/showGoods"||path=="/adminInfo"||path=="/findAdminInfo"||path=="/dealGoods"||path=="/goods"||path=="/main"||path=="/noPermission"){
        next()
    }else{
        let route = JSON.parse(sessionStorage.getItem("route"))
        console.log(route)
        if(route){
          let bool = true
          for (var i = 0; i < route.length; i++) {
            let t = route[i]
            console.log(t+":"+path)
            if(t==path){
              bool = false
              console.log(bool)
              next()
            }
          }
          if(bool){
            next("/noPermission")
          }
        }else{
          next("/noPermission")
        }
    }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
